﻿namespace EasyToolkit.Excel.BindingTemplates.Controls.CheckBox
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using EasyToolkit.BindingTemplates.Context;
    using EasyToolkit.BindingTemplates.Definitions.Binding;
    using EasyToolkit.Excel.BindingTemplates.Definitions;
    using EasyToolkit.Tools.Extensions;

    class ExcelBindingDefinitionCheckBox : BindingDefinition
    {
        #region attributes and properties
        public const string CHECKBOX_TEMPLATE_PREFIX = "<CheckBox";

        public IBindingDefinition ValueBindingDefinition
        { get; private set; }

        public ExcelCheckBoxDefinition Definition
        { get; private set; }

        public ExcelTemplateDefinitionPart TemplateDefinition
        { get; private set; }
        #endregion

        #region .ctors and factories
        private ExcelBindingDefinitionCheckBox(ExcelTemplateDefinitionPart templateDefinition, ExcelCheckBoxDefinition definition)
        {
            TemplateDefinition = templateDefinition;
            Definition = definition;
            DefinitionDescription = BindingDefinitionDescription.CreateBindingDescription(Definition.Value, Definition.Value);
            if (!string.IsNullOrEmpty(definition.Value))
            {
                ValueBindingDefinition = BindingDefinitionFactory.CreateCommonBindingDefinition(templateDefinition, DefinitionDescription);
                if (! ValueBindingDefinition.BindingType.Equals(typeof(bool)))
                    throw new EasyToolkitException("A 'CheckBox' must be bound with RetrieveContextualMethodInfo boolean value");

                CanNotify = ValueBindingDefinition.CanNotify;
            }
        }

        static public ExcelBindingDefinitionCheckBox CreateInstance(ExcelTemplateDefinitionPart templateDefinition, string definition)
        {
            ExcelBindingDefinitionCheckBox ret = null;
            if (! string.IsNullOrEmpty(definition))
            {
                try
                {
                    ExcelCheckBoxDefinition excelButtonDefinition = definition.Deserialize<ExcelCheckBoxDefinition>();
                    ret = new ExcelBindingDefinitionCheckBox(templateDefinition, excelButtonDefinition);
                }
                catch (Exception ex)
                {
                    string message = string.Format("Cannot retrieve the checkbox definition '{0}'. {1}", definition.EmptyIfNull(), ex.Message);
                    throw new EasyToolkitException(message, ex);
                }
            }
            return ret;
        }
        #endregion

        #region public methods
        public override object UpdateDataSource(object dataSource, object data)
        {
            return ValueBindingDefinition == null ? null : ValueBindingDefinition.UpdateDataSource(dataSource, data);
        }

        public override object ResolveBinding(object dataSource)
        {
            return ValueBindingDefinition == null ? null : ValueBindingDefinition.ResolveBinding(dataSource);
        }

        public override IBindingContextItem ContextItemFactory(IBindingContextElement parent)
        {
            return new ExcelContextItemCheckBox(parent, this);
        }

        public override IEnumerable<INotifyPropertyChanged> GetObjectsToNotify(object dataSource)
        {
            return ValueBindingDefinition == null ? null : ValueBindingDefinition.GetObjectsToNotify(dataSource);
        }

        public override bool MustNotify(object dataSource, object source, PropertyChangedEventArgs args)
        {
            return ValueBindingDefinition == null ? false : ValueBindingDefinition.MustNotify(dataSource, source, args);
        }
        #endregion
    }
}
